package com.bootdo.wx.api;


import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.ImgUpload;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.wx.service.SetImageUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author changwj changwj0120@163.com
 */
@RequestMapping("/api/common")
@Controller
public class ApiCommonController {
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    FileService sysFileService;
    @Autowired
    BootdoConfig bootdoConfig;
    @Autowired
    private SetImageUploadService setImageUploadService;

    @ResponseBody
    @RequestMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String fileName = null;
//        fileName = FileUtil.renameToUUID(fileName);
//        FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image != null) {//如果image=null 表示上传的不是图片格式
//			    System.out.println(image.getWidth());//获取图片宽度，单位px
//			    System.out.println(image.getHeight());//获取图片高度，单位px
                fileName = "IMG_" + image.getWidth() + "_" + image.getHeight() + "@"+ UUID.randomUUID().toString();;
            }else {
                fileName = UUID.randomUUID().toString();
            }
            String imagePath = new ImgUpload(setImageUploadService).upload(file.getBytes(),
                    fileName);
//            FileDO sysFile = new FileDO(FileType.fileType(fileName), imagePath, new Date());
            if (StringUtils.isBlank(imagePath)) {
                return  R.error("上传失败");
            }
            return R.ok().put("data", imagePath);
        } catch (Exception e) {
            return R.error();
        }
    }

    @ResponseBody
    @RequestMapping("/uploads")
    public R uploads(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        try {
        List<String> list = new ArrayList<String>();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];

                String fileName = null;
                BufferedImage image = ImageIO.read(file.getInputStream());
                if (image != null) {//如果image=null 表示上传的不是图片格式
//			    System.out.println(image.getWidth());//获取图片宽度，单位px
//			    System.out.println(image.getHeight());//获取图片高度，单位px
                    fileName = "IMG_" + image.getWidth() + "_" + image.getHeight() + "@"+UUID.randomUUID().toString();;
                }else {
                    fileName = UUID.randomUUID().toString();
                }
//                FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath(), fileName);
                String imagePath = new ImgUpload(setImageUploadService).upload(file.getBytes(),
                        fileName);
                // 保存文件
                list.add(imagePath);
            }
        }
            return R.ok().put("data", list);
//        FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
        } catch (Exception e) {
            return R.error();
        }

    }

}
